﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace MyBasicLib.Collections.Generic
{
    /// <summary>
    /// 分页列表
    /// </summary>
    /// <typeparam name="T"></typeparam> 
    [DataContract]
    public class PagedList<T> : IPagedList//,IEnumerable<T>
    {
        public PagedList()
            : this(new T[0], 1, 10, 0)
        {
        }
        /// <summary>
        /// 分页列表构造函数
        /// </summary>
        /// <param name="source">源</param>
        /// <param name="index">页码,从1开始</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="totalCount">总记录数</param>
        public PagedList(IList<T> source, int index, int pageSize, int totalCount)
        //: base(source)
        {
            InnerList = source;
            if (index < 1)
                index = 1;
            PageSize = pageSize;
            PageIndex = index;

            TotalCount = totalCount;
            TotalPages = TotalCount / PageSize;
            if (TotalCount > TotalPages * PageSize)
                ++TotalPages;
            if (PageIndex > TotalCount) {
                PageIndex = TotalCount;
            }
        }
        [DataMember]
        public IList<T> InnerList { get; set; }

        [DataMember]
        public int TotalPages { get; set; }

        [DataMember]
        public int TotalCount { get; set; }

        [DataMember]
        public int PageIndex { get; set; }

        [DataMember]
        public int PageSize { get; set; }
         
        public bool HasPreviousPage
        {
            get { return PageIndex > 1; }
        } 
        public bool HasNextPage
        {
            get { return PageIndex < TotalPages; }
        }
         
        public int Count
        {
            get { return InnerList.Count; }
        } 

        public T this[int index]
        {
            get { return InnerList[index]; }
        }
    }

}